﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using Cofe.Core;
using Cofe.Core.Utils;
using Cofe.Web.Utils;
using Newtonsoft.Json.Linq;

namespace Cofe.Web.Controllers.Api
{
    public class EntryListController : ApiController
    {


        // POST api/entrylist
        [HttpPost]
        [Authorize]
        public async Task<HttpResponseMessage> UpdateEntries(HttpRequestMessage req)
        {
            var r = CofeWsServices.HttpMessageFormatter.CreateRequestObject<WebEntryList>(req);

            foreach (var e in r.Entries)
                await e.Properties.Behaviors.CommitChangesAsync();

            var webResourceAttrib = AttributeUtils<WebResourceAttribute>.FindAttribute(CofeDirectoryProperties.List);
            return CofeWsServices.HttpMessageFormatter.CreateResponse(Request, r, r.Parameters, new ResponseFormatterInfo()
            {
                StatusCode = HttpStatusCode.OK,
                ResourceAttribute = webResourceAttrib
            });
        }

        // POST api/entrylist/AddTag
        [HttpPost]
        [Authorize]
        public HttpResponseMessage ExecuteAction(HttpRequestMessage req, string resource)
        {
            string action = resource;

            var r = CofeWsServices.HttpMessageFormatter.CreateRequestObject<WebEntryList>(req);


            object property = CofeServices.PropertyDictionary.GetProperty(action);
            if (property == null)
                throw new HttpResponseException(HttpStatusCode.BadRequest);

            if (!(DecoratorExtension.IsWebAction(property)) || !(DecoratorExtension.IsAction(property)))
                throw new HttpResponseException(HttpStatusCode.BadRequest);

            Type outputType; Object outputObj;

            try
            {
                foreach (var e in r.Entries)
                {
                    e.Properties.Behaviors.ResetChanges();
                    ParameterDic pd = ControllerUtils.getParamDicFromQueryString(property);
                    e.Properties.Behaviors.InvokeNonGeneric(property, pd, out outputType, out outputObj);
                    //e.Properties.Behaviors.Invoke(CofeProperties.RefreshAsync);
                }
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }

            var webResourceAttrib = AttributeUtils<WebResourceAttribute>.FindAttribute(CofeDirectoryProperties.List);
            return CofeWsServices.HttpMessageFormatter.CreateResponse(Request, r, r.Parameters, new ResponseFormatterInfo()
            {
                StatusCode = HttpStatusCode.OK,
                ResourceAttribute = webResourceAttrib
            });
        }

        //[HttpPost]
        //public HttpResponseMessage Post(string resource)
        //{
        //    return new HttpResponseMessage(HttpStatusCode.InternalServerError);
        //}

        //// PUT api/entrylist/5
        //public void Put(int id, [FromBody]string value)
        //{
        //}

        //// DELETE api/entrylist/5
        //public void Delete(int id)
        //{
        //}
    }
}
